#!/bin/bash
function tar_docker() {
    if [[ ! -d docker ]];then
        echo "正在解压docker安装包"
        tar -xvf docker*.tgz
    fi
}
function install() {
    install_path=$1
    install_server=$2
    if [[ $2 == "local"  ]];then
        echo "将在当前服务器安装docker！"
        if [[ -f /usr/bin/containerd ]];then
            rm -rf /usr/bin/containerd
        fi
        if [[ -f /usr/bin/dockerd ]];then
            rm -rf /usr/bin/dockerd
        fi
        cp docker/* /usr/bin
        if [[ -f /etc/systemd/system/docker.service ]];then
            rm -rf /etc/systemd/system/docker.service
        fi
        cp docker.service /etc/systemd/system/
        chmod +x /etc/systemd/system/docker.service
        if [[ ! -d /etc/docker ]];then
            mkdir /etc/docker
        fi
        sed -i "s#{data_path}#$1#g" daemon.json
        if [[ -f /etc/docker/daemon.json ]];then
            rm -rf /etc/docker/daemon.json
        fi
        cp daemon.json /etc/docker/
        systemctl daemon-reload
        systemctl start docker
        a=$(ls -l |grep docker-compose|awk '{print $9}')
        mv $a docker-compose
        if [[ -f /usr/bin/docker-compose ]];then
            rm -rf /usr/bin/docker-compose
        fi
        cp docker-compose /usr/bin
        chmod +x /usr/bin/docker-compose
        docker version
        docker-compose version
        rm -rf docker
        exit
    fi

    if [[ $2 ]];then
        echo "将在服务器 $2 安装docker！"
        echo "正在拷贝docker安装程序到服务器 $2 "
        ssh $2 "mkdir -p /data/software/docker"
        scp -r ./ root@$2:/data/software/docker
        ssh $2 "cd /data/software/docker && sh install.sh $1 local"
    fi
}
function delete_docker() {
    rm -rf docker
}

tar_docker
if [[ $2 ]];then
    install $1 $2
fi
if [[ $3 ]];then
    install $1 $3
fi
if [[ $4 ]];then
    install $1 $4
fi
if [[ $5 ]];then
    install $1 $5
fi
if [[ $6 ]];then
    install $1 $6
fi
if [[ $7 ]];then
    install $1 $7
fi
if [[ $8 ]];then
    install $1 $8
fi
delete_docker